package com.example.controller;

import com.example.model.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @RequestMapping("/save")
    public String saveUserInfo(UserInfo userInfo, HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("user",userInfo);
        List<String> kecheng = new ArrayList<>();
        kecheng.add("计算机");
        kecheng.add("语文");
        kecheng.add("数学");
        kecheng.add("英语");
        kecheng.add("政治");
        session.setAttribute("kecheng",kecheng);
        return "redirect:xuanke.html";
    }

    @RequestMapping("/getKecheng")
    @ResponseBody
    public List<String> getKecheng(HttpServletRequest request){
        HttpSession session = request.getSession();

        List<String> kecheng = (List<String>) session.getAttribute("kecheng");

        return kecheng;
    }

    @RequestMapping("/check")
    @ResponseBody
    public Map<String,Object> check(UserInfo userInfo){
        Map<String,Object> data = new HashMap<>();
        if (userInfo!=null){
            if (StringUtils.isEmpty(userInfo.getName())){
                data.put("code",1);
                data.put("msg","姓名不能为空");
                return data;
            }
            if (StringUtils.isEmpty(userInfo.getStudentId())){
                data.put("code",2);
                data.put("msg","学号不能为空");
                return data;
            }
            if (StringUtils.isEmpty(userInfo.getGrade())){
                data.put("code",3);
                data.put("msg","年级不能为空");
                return data;
            }
            if (StringUtils.isEmpty(userInfo.getMajor())){
                data.put("code",4);
                data.put("msg","专业不能为空");
                return data;
            }
            if (StringUtils.isEmpty(userInfo.getCollege())){
                data.put("code",5);
                data.put("msg","学院不能为空");
                return data;
            }
        }
        data.put("code",0);
        return data;
    }

    @RequestMapping("/setResult")
    public String setResult(@RequestBody ArrayList<String> list, HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("xuanke",list);
        return "redirect:result.html";
    }
    @RequestMapping("/getResult")
    @ResponseBody
    public Map<String,Object> getResult(HttpServletRequest request){
        HttpSession session = request.getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute("user");
        List<String> list = (List<String>) session.getAttribute("xuanke");
        Map<String,Object> data = new HashMap<>();
        data.put("user",userInfo);
        data.put("xuanke",list);

        return data;
    }
}
